package org.firestorm.traficforecast.utils;

import java.io.File;

import org.firestorm.traficforecast.database.MyNode;

public class CommonUtils {
	public static void main(String[] args) {
		MyNode node1 = new MyNode();
		node1.setLat(21.0494765);
		node1.setLon(105.8777853);

		MyNode node2 = new MyNode();
		node2.setLat(21.0495647);
		node2.setLon(105.8780342);

		System.out.println(distance(node1, node2));
		double now = Math.random();

		System.out.println(now);
		long noew = Math.round(Math.random() * 1000);
		System.out.println(noew);

	}

	public static String createDir() {
		String os_name = System.getProperty("os.name");
		String filePath = "";
		if (os_name.contains("Windows")) {
			filePath = "C:\\traficforecast";
			File theDir = new File(filePath);
			if (!theDir.exists()) {
				try {
					theDir.mkdir();
				} catch (SecurityException se) {
					return "";
				}
			}else{
				return "C:\\traficforecast";
			}
		} else if (os_name.contains("Linux")) {
			filePath = "/traficforecast";
			File theDir = new File(filePath);
			if (!theDir.exists()) {
				try {
					theDir.mkdir();
				} catch (SecurityException se) {
					return "";
				}
			}else{
				return "/traficforecast";
			}
		} else {
			return "";
		}
		return filePath;
	}

	public static double distance(MyNode a, MyNode b) {
		double lat1 = a.getLat() * Math.PI / 180;
		double lat2 = b.getLat() * Math.PI / 180;
		double deltaLat = Math.abs(lat1 - lat2);
		double deltaLon = Math.abs((a.getLon() - b.getLon())) * Math.PI / 180;
		double a_ = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
				+ Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLon / 2)
				* Math.sin(deltaLon / 2);
		double c_ = 2 * Math.atan2(Math.sqrt(a_), Math.sqrt(1 - a_));
		double d = 6371009 * c_;
		return d;
	}
}
